
clear
set more off

/*********************************************************************************
Name: Data/Intermediate/patient_controls.do

Data In: [Data/Original/patient_surveys.dta]

Data Out: [Data/Intermediate/patient_controls.dta]

Results Out: []

Purpose of do-file: Creating a database of patient-level control variables
*********************************************************************************/

* Setting path directory
cd "${DIRECTORY}"


** Creating control variables from patient surveys

use "Data/Original/patient_surveys.dta", clear

* Gender
ta a1_gender, m
gen pat_male = (a1_gender == 1) if a1_gender ~= . 

* Age
ta a3_age, m
gen pat_age = a3_age if a3_age ~= . & a3_age ~= -999

* Caste
ta a4_caste, m
gen pat_caste_general = (a4_caste == 1) if a4_caste ~=. & a4_caste ~= -999

ta a4_caste, m
gen pat_caste_sc = (a4_caste == 3) if a4_caste ~=. & a4_caste ~= -999

* Religion
ta a5_religion, m
gen pat_hindu = (a5_religion==1) if a5_religion ~= . 

* Literacy: patient can read and write
ta a6_read_write, m
gen pat_rw_both = (a6_read_write == 1) if a6_read_write ~= .

* Owns their house
ta q19_own_house, m
gen pat_ownhouse = (q19_own_house == 1) if q19_own_house != . & q19_own_house != -999

* Patient has electricity
tab q9_have_electricity, m
gen pat_elec = (q9_have_electricity == 1) if q9_have_electricity != . & q9_have_electricity != -999

* Patient's household size
ta a20_people_live_hh a19_live_alone, m
gen pat_hhd_size = 1 if a19_live_alone == 1
replace pat_hhd_size = a20_people_live_hh + 1 if pat_hhd_size == . & a20_people_live_hh ~= .

* Migration
replace a22_live_neigh=. if  a22_live_neigh==-999
ta a22_live_neigh, m

gen pat_migrate_always = (a22_live_neigh == 1) if a22_live_neigh ~= .
gen pat_migrate_6plus = (a22_live_neigh == 2 | a22_live_neigh == 3) if a22_live_neigh ~= .

* Time to center
ta i14_asha_counselor1, m
gen pat_time_to_center = i14_asha_counselor1 if i14_asha_counselor1 != . & i14_asha_counselor1 != -999


** Completing values of control variables using another observation from the same patient, when missing

foreach var of varlist pat_male - pat_time_to_center {
so q1_ques_code `var'
replace `var' = `var'[_n-1] if `var' == . & q1_ques_code == q1_ques_code[_n-1]
}


** Keeping only one observation per patient

so q1_ques_code a02_entry_exit
by q1_ques_code: gen temp = _n
keep if temp == 1

keep q1_ques_code pat_male - pat_time_to_center


** Replacing by 0's when missing, and creating dummies indicating this

foreach var in male age caste_general caste_sc hindu rw_both ownhouse elec hhd_size migrate_always migrate_6plus time_to_center{
gen pat_dum_`var' = (pat_`var' == .)
replace pat_`var' = 0 if pat_`var' == .
}

save "Data/Intermediate/patient_controls.dta", replace 